package com.book.service;

import java.io.IOException;
import java.net.SocketException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.book.constant.Constants;
import com.book.dao.BookDao;
import com.book.formbean.BookForm;
import com.book.formbean.UserForm;
import com.book.model.BigType;
import com.book.model.Book;
import com.book.model.BookPage;
import com.book.model.SmallType;
import com.book.model.Type;
import com.book.service.io.EncryptedInputStream;

@Component
@SuppressWarnings("static-access")
public class BookService {

	@Autowired
	private BookDao bookDao;

	@Autowired
	private FtpService ftpService;

	public BookPage findBookPage(BookForm bookForm) {
		BookPage bookPage = new BookPage();
		bookPage.setBookCount(bookDao.findBookCount());
		List<Book> books = bookDao.findBooks(bookForm.getTitle(), getBookIds(bookForm.getType1(), bookForm.getType2(), bookForm.getType3()), bookForm.getAuthor(), bookForm.getPageNo(), bookForm
				.getPageSize());
		setBookTypes(books);
		bookPage.setBooks(books);
		return bookPage;
	}

	public void addBook(BookForm bookForm) throws IOException {
		String bookId = bookDao.addBook(bookForm, findSmallTypes(bookForm.getType1(), bookForm.getType2(), bookForm.getType3()));
		String folderName = getFolderName(Integer.valueOf(bookId));
		ftpService.uploadFile(Constants.FTP_URL, 21, Constants.FTP_USER, Constants.FTP_PASSWORD, folderName, bookForm.getFileData().getOriginalFilename(), new EncryptedInputStream(bookForm
				.getFileData().getInputStream()));
	}

	public void delBook(Book book) throws SocketException, IOException {
		String folderName = getFolderName(Integer.valueOf(book.getId()));
		ftpService.delFtpFile(Constants.FTP_URL, 21, Constants.FTP_USER, Constants.FTP_PASSWORD, folderName, book.getPath());
		bookDao.delBook(book.getId());
		bookDao.delBookToTypeByBookId(book.getId());
	}

	public boolean isRightUser(UserForm userForm) {
		return bookDao.findUniqueUser(userForm) > 0;
	}

	public Type getType() {
		Type type = new Type();
		BigType bigType1 = bookDao.findBigTypes().get(0);
		BigType bigType2 = bookDao.findBigTypes().get(1);
		BigType bigType3 = bookDao.findBigTypes().get(2);
		type.setType1s(bookDao.findSmallTypes(bigType1.getId()));
		type.setType2s(bookDao.findSmallTypes(bigType2.getId()));
		type.setType3s(bookDao.findSmallTypes(bigType3.getId()));
		type.setBigtype1(bigType1.getName());
		type.setBigtype2(bigType2.getName());
		type.setBigtype3(bigType3.getName());
		return type;
	}

	public String getBookIds(String type1, String type2, String type3) {
		String types = findSmallTypes(type1, type2, type3);
		if (types.equals("ALL")) {
			return Constants.ALL;
		}
		return bookDao.findBookIds(types.substring(0, types.length() - 1));
	}

	private String findSmallTypes(String type1, String type2, String type3) {
		StringBuilder types = new StringBuilder("");
		if (!Constants.ALL.equals(type1) && type1 != null) {
			types.append(type1 + ",");
		}
		if (!Constants.ALL.equals(type2) && type2 != null) {
			types.append(type2 + ",");
		}
		if (!Constants.ALL.equals(type3) && type3 != null) {
			types.append(type3 + ",");
		}
		if (types.toString().equals("")) {
			return Constants.ALL;
		}
		return types.toString();
	}

	private void setBookTypes(List<Book> books) {
		for (Book book : books) {
			StringBuffer sb = new StringBuffer();
			List<SmallType> smallTypes = bookDao.findSmallTypesByBookId(book.getId());
			for (SmallType smallType : smallTypes) {
				sb.append(smallType.getName() + ",");
			}
			book.setType(sb.length() == 0 ? "" : sb.toString().substring(0, sb.length() - 1));
		}
	}

	public String getFolderName(int bookId) {
		return getFolderName(bookDao.findSmallTypesByBookId(bookId));
	}

	public String getFolderName(List<SmallType> smallTypes) {
		String[] smallTypeIds = new String[30];

		for (SmallType smallTypeValue : smallTypes) {
			smallTypeIds[smallTypeValue.getId() - 1] = "1";
		}

		StringBuffer sb = new StringBuffer();
		for (String idValue : smallTypeIds) {
			if (idValue == null) {
				sb.append("0");
			} else {
				sb.append(idValue);
			}
		}
		return sb.toString();

	}
}
